<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicModal
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'unitmanage:equipmentSelection:edit'"
    @register="registerModalDevice"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup name="ViewsUnitmanageEquipmentSelectionForm">
  import { ref, unref, computed,defineProps } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { EquipmentSelection, equipmentSelectionSave, equipmentSelectionForm,equipmentSelectionListData } from '/@/api/unitmanage/equipmentSelection';
  
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('unitmanage.equipmentSelection');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<EquipmentSelection>({} as EquipmentSelection);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('火情展示设备配置') : t('火情展示设备配置'),
  }));

  const props=defineProps({
    record: {
      name: String,
      unitCode: String,
    }
  })

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('消防设备类型'),
      field: 'equipmentType',
      fieldLabel:'equipmentName',
      component: 'TreeSelect',
      componentProps: {
        dictType: 'fire_equipment_selection',
        allowClear: true,
        treeCheckable: true,
        defaultOpen: true,
      },
      required: true,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 20 },
  });

  const [registerModalDevice, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ loading: true });
    await resetFields();
    data={enterpriseCode:props.record.unitCode}
    const res = await equipmentSelectionListData(data);
    console.log('data', data);
    record.value = (res.list[0] || {}) ;
    console.log('res', res);
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setModalProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      let data = await validate();
      data={
        ...data,
        enterpriseName: props.record.name,
        enterpriseCode: props.record.unitCode,
        tid: record.value.tid,
      }
      setModalProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
      };

      // console.log('submit', params, data, record);
      const res = await equipmentSelectionSave(params, data);
      showMessage(res.message);
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
